package com.sy.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	public void validate() {
		if(this.getUsername()==null ||"".equals(this.getUsername().trim())){
			this.addFieldError("username", "用户名不能为空！");
		}
		if(this.getPassword()==null ||"".equals(this.getPassword().trim())){
			this.addFieldError("password", "密码不能为空！");
		}
	}

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@SuppressWarnings("unchecked")
	public String execute() throws Exception{//自动执行
		
		if("shiyang".equals(this.getUsername().trim())){
			if("123".equals(this.getPassword().trim())){
				
				@SuppressWarnings("unused")
				Map map=ActionContext.getContext().getSession();//插入Session的值
				map.put("user", "shiyang");
				
				
				return "success";
			}else{
				this.addFieldError("password", "密码错误！");
				return "failure";
			}
		}else{
			this.addFieldError("username", "用户名错误！");
			return "failure";
		}
	}
}
